<?php
include('./include/session_verify.php');
include('../include/db.config.php');
@$id = $_GET['id'];
@$opera = $_GET['opera'];
@$sql_id = $_GET['sql_id'];
@$name = $_GET['name'];
@$category = $_GET['category'];
@$value = $_GET['value'];
@$files = $_GET['files'];
@$description = $_GET['description'];
@$password=$_GET['password'];
@$email = $_GET['email'];
@$number =$_GET['number'];
@$phone = $_GET['phone'];
@$school =$_GET['school'];
@$student_id = $_GET['student_id'];
@$team_id = $_GET['team_id'];
@$content =$_GET['content'];
@$answer = $_GET['answer'];
@$enable = $_GET['enable'];
@$radio = $_GET['radio'];
@$member_id = $_GET['member_id'];
@$member_name = $_GET['member_name'];
@$flag = $_GET['flag'];
function state_check($result){
	if($result){
		$state = 200;
		return $state;
	}else{
		$state = 400;
		return $state;
	}
}


if($id == 1){
	if ($opera==1) {
		$sql = "INSERT INTO challenge (name, category,value,files,description,flag) VALUES ('".$name."','".$category."','".$value."','".$files."','".$description."','".$flag."')";
		echo state_check(mysql_query($sql));
	}elseif($opera==2){
		$sql = "DELETE FROM challenge WHERE id='".$sql_id."'";
		echo state_check(mysql_query($sql));
	}else if($opera==3){
		$sql = "UPDATE challenge SET name ='".$name."',category='".$category."', value='".$value."',files='".$files."',description='".$description."' WHERE id='".$sql_id."'";
		echo state_check(mysql_query($sql));
	}
}elseif($id == 2){
	if ($opera == 1) {
		$sql = "INSERT INTO team (name, email,password,number,phone,school) VALUES ('".$name."','".$email."','".$password."','".$number."','".$phone."','".$school."')";
		mysql_query($sql);
		$sql2 = "select * from team where name='".$name."'";
		$result = getOne($sql2);
		$sql3 = "INSERT INTO member (student_id,name,team_id) VALUES ('".$student_id."','".$member_name."','".$result['id']."')";
		echo state_check(mysql_query($sql3));
	}elseif($opera == 2){
		$sql = "DELETE FROM team WHERE id='".$sql_id."'";
		$sql1 = "DELETE FROM member WHERE team_id='".$sql_id."'";
		mysql_query($sql1);
		echo state_check(mysql_query($sql));
	}else if($opera == 3 ){
		if($password != ""){
			$sql = "UPDATE team SET name ='".$name."',email='".$email."', password='".$password."',number='".$number."',phone='".$phone."',school='".$school."' WHERE id ='".$sql_id."'";

			echo state_check(mysql_query($sql));
		}else{
			$sql = "UPDATE team SET name ='".$name."',email='".$email."',number='".$number."',phone='".$phone."',school='".$school."' WHERE id ='".$sql_id."'";
			
			echo state_check(mysql_query($sql));
		}
	}elseif ($opera == 4) {
		if(isset($student_id)){
			$sql = "select * from member where student_id='".$student_id."'";
			$result_member = getOne($sql);
			$sql1 = "select * from team where (number<3 and status='1') or id=".$result_member['team_id'];
			$result = getAll($sql1);
			echo json_encode($result);
		}else{
			$sql1 = "select * from team where (number<3 and status='1')";
			$result = getAll($sql1);
			echo json_encode($result);
		}
	}elseif($opera == 5){
		$sql = "select * from member where id=".$member_id;
		$result = getOne($sql);
		echo json_encode($result);
	}
}elseif ($id==3) {
	if ($opera==1) {
		$sql = "INSERT INTO member (student_id,name,team_id) VALUES ('".$student_id."','".$name."','".$team_id."')";
		$sql1 = "select * from team where id=".$team_id;
		$result_team = getOne($sql1);
		$number = $result_team['number'];
		$number +=1;
		$sql2 = "UPDATE team SET number='".$number."' WHERE id ='".$team_id."'";
		mysql_query($sql2);
		echo state_check(mysql_query($sql));
	}elseif($opera==2){
		$sql = "select * from member where id=".$sql_id;
		$result_member = getOne($sql);
		$team_id = $result_member['team_id'];
		$sql1 = "select * from team where id=".$team_id;
		$result_team = getOne($sql1);
		$number = $result_team['number'];
		$number -=1;
		if($number == 0){
			$sql2 = "DELETE FROM team WHERE id='".$team_id."'";
			mysql_query($sql2);
		}else{
			$sql2 = "UPDATE team SET number='".$number."' WHERE id ='".$team_id."'";
			mysql_query($sql2);
		}
		$sql3 = "DELETE FROM member WHERE id='".$sql_id."'";
		echo state_check(mysql_query($sql3));

	}elseif ($opera==3) {
		$sql = "select * from member where id=".$sql_id;
		$result_member = getOne($sql);
		if($result_member['team_id'] == $team_id){
			$sql2 = "UPDATE member SET name ='".$name."',student_id='".$student_id."' WHERE id ='".$sql_id."'";
			echo state_check(mysql_query($sql2));
		}else{
			$sql2 = "select * from team where id=".$team_id; //现在的team_id
			$result_surr = getOne($sql2);
			$sql3 = "select * from team where id=".$result_member['team_id'];//原来的team_id
			$result_orgin = getOne($sql3);
			$number_orgin = $result_orgin['number'];
			$number_surr = $result_surr['number'];
			$number_orgin -=1;
			$number_surr +=1;
			if($number_orgin == 0){
				$sql4 = "DELETE FROM team WHERE id='".$result_member['team_id']."'";
				mysql_query($sql4);
				$sql5 = "UPDATE team SET number='".$number_surr."' WHERE id ='".$team_id."'";
				mysql_query($sql5);
			}else{
				$sql4 = "UPDATE team SET number='".$number_orgin."' WHERE id ='".$result_member['team_id']."'";
				mysql_query($sql4);
				$sql5 = "UPDATE team SET number='".$number_surr."' WHERE id ='".$team_id."'";
				mysql_query($sql5);
			}
			$sql2 = "UPDATE member SET name ='".$name."',student_id='".$student_id."',team_id='".$team_id."' WHERE id ='".$sql_id."'";
			echo state_check(mysql_query($sql2));
		}
	}
}elseif ($id==4){
	if($opera==2){
		$sql = "DELETE FROM member WHERE team_id='".$sql_id."'";
		mysql_query($sql);
		$sql1 = "DELETE FROM team WHERE id='".$sql_id."'";
		echo state_check(mysql_query($sql1));
	}elseif ($opera==3) {
		$sql = "UPDATE team SET status ='1' WHERE id ='".$sql_id."'";
		echo state_check(mysql_query($sql));
	}

}elseif ($id==5){
	if($opera==1){
		$sql = "INSERT INTO challenge_radio (content,answer,enable,radio) VALUES ('".$content."','".$answer."','".$enable."','".$radio."')";
		echo state_check(mysql_query($sql));
	}
	if($opera==2){
		$sql = "DELETE FROM challenge_radio WHERE id=".$sql_id;
		echo state_check(mysql_query($sql));
	}elseif ($opera==3) {
		$sql = "UPDATE challenge_radio SET content ='".$content."',answer='".$answer."',enable=".$enable.",radio='".$radio."' WHERE id=".$sql_id;
		echo state_check(mysql_query($sql));
	}elseif($opera == 4){
		$sql = "select * from challenge_radio where id='".$sql_id."'";

		$result = getOne($sql);
		echo json_encode($result);
	}

}